/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package local.servlets;

import java.io.IOException;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import local.utils.SQL;

/**
 *
 * @author Olenkita
 */
public class Contenido extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if(form(request, response)==false) {
            error(request, response);
            return;
        }
        if(action(request, response)==false) {
            error(request, response);
            return;
        }
        view(request, response);
    }

    private boolean form(HttpServletRequest request, HttpServletResponse response) {
        String botonEnviarConsulta = request.getParameter("enviar");
        if(botonEnviarConsulta!=null) {
            String textConsulta = request.getParameter("textConsulta");
            if(textConsulta.isEmpty()) {
                request.setAttribute("error", "Debe escribir para buscar");
                return false;
            }
            if(!(textConsulta.equals("clients") || textConsulta.equals("employees") || textConsulta.equals("providers"))) { 
                request.setAttribute("error", "Palabra no valida para buscar");
                return false;
            }
        }
        //
        int c;
        return true;
    }
    private boolean action(HttpServletRequest request, HttpServletResponse response) {
        String botonEnviarConsulta = request.getParameter("enviar");
        String textConsulta = request.getParameter("textConsulta");
        
        if(botonEnviarConsulta!=null) {
            List lista = null;
            try {
                lista=SQL.getContenidoTabla(textConsulta);
            } catch(Exception ex) {
                request.setAttribute("error", "EXCEPCIÓN CONTROLADA");
                return false;
            }
            request.setAttribute("a", lista);
        }
        return true;
    }
    private void view(HttpServletRequest request, HttpServletResponse response) {
        ServletContext context = this.getServletContext();
        RequestDispatcher view = context.getRequestDispatcher("/views/v_S1.jsp");
        try {
            view.forward(request, response);
        } catch(ServletException ex) {
        } catch(IOException ex) {}
    }
    private void error(HttpServletRequest request, HttpServletResponse response) {
        ServletContext context = this.getServletContext();
        RequestDispatcher view = context.getRequestDispatcher("/index.jsp");
        try {
            view.forward(request, response);
        } catch(ServletException ex) {
        } catch(IOException ex) {}
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
